﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F501_Danh_muc_san_pham : Form
    {
        public F501_Danh_muc_san_pham()
        {
            InitializeComponent();
            set_define_events();
        }
        #region Public Interfaces

        #endregion

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();     
        #endregion

        #region Private Methods
        private void set_define_events()
        {
            this.Load += F501_Danh_muc_san_pham_Load;
        }

        private bool check_data_validate_is_ok()
        {
            if (string.IsNullOrWhiteSpace(m_txt_ma_san_pham.Text))
            {
                MessageBox.Show("Bạn phải nhập mã sản phẩm!", "Thông báo!");
                m_txt_ma_san_pham.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(m_txt_ten_san_pham.Text))
            {
                MessageBox.Show("Bạn phải nhập tên sản phẩm!", "Thông báo!");
                m_txt_ten_san_pham.Focus();
                return false;
            }
            return true;
        }
        
        private void load_data_to_grid()
        {
            //var v_lst_so_du = db.GD_SO_DU
            //    .OrderBy(x => x.DM_SAN_PHAM.MA_SAN_PHAM)
            //    .ToList();
            //var v_lst_danh_muc_san_pham = db.DM_SAN_PHAM
            //   .Where(x => x.MA_SAN_PHAM.Contains(m_txt_tim_kiem_san_pham.Text.Trim())
            //   || x.TEN_SAN_PHAM.Contains(m_txt_tim_kiem_san_pham.Text.Trim())
            //   || m_txt_tim_kiem_san_pham.Text.Equals(""))
            //   .OrderBy(x => x.MA_SAN_PHAM)
            //   .Select(x => new
            //   {
            //       ID = x.ID,
            //       MA_SAN_PHAM = x.MA_SAN_PHAM,
            //       TEN_SAN_PHAM = x.TEN_SAN_PHAM,
            //       SO_LUONG = v_lst_so_du.Where(sd => sd.ID_SAN_PHAM == x.ID).Select(sd => sd.SO_LUONG),
            //       DON_VI_TINH = x.DON_VI_TINH
            //   })
            //   .ToList();
            var v_lst_danh_muc_san_pham = db.GD_SO_DU
                .Where(x => x.DM_SAN_PHAM.MA_SAN_PHAM.Contains(m_txt_tim_kiem_san_pham.Text.Trim())
                || x.DM_SAN_PHAM.TEN_SAN_PHAM.Contains(m_txt_tim_kiem_san_pham.Text.Trim())
                || m_txt_tim_kiem_san_pham.Text == "")
                .GroupBy(x => x.ID_SAN_PHAM)
                .Select(x => x.OrderByDescending(y => y.NGAY_THANG).FirstOrDefault())
                .OrderBy (x => x.DM_SAN_PHAM.MA_SAN_PHAM)
                .Select(x => new
                {
                    ID = x.ID,
                    MA_SAN_PHAM = x.DM_SAN_PHAM.MA_SAN_PHAM,
                    TEN_SAN_PHAM = x.DM_SAN_PHAM.TEN_SAN_PHAM,
                    x = x.SO_LUONG,
                    DON_VI_TINH = x.DM_SAN_PHAM.DON_VI_TINH
                })
                .ToList();
            m_grv_san_pham.DataSource = v_lst_danh_muc_san_pham;
            m_grv_san_pham.Columns[0].HeaderText = "ID";
            m_grv_san_pham.Columns[0].Width = 20;
            m_grv_san_pham.Columns[0].Visible = false;
            m_grv_san_pham.Columns[1].HeaderText = "Mã sản phẩm";
            m_grv_san_pham.Columns[1].Width = 100;
            m_grv_san_pham.Columns[2].HeaderText = "Tên sản phẩm";
            m_grv_san_pham.Columns[2].Width = 200;
            m_grv_san_pham.Columns[3].HeaderText = "Số lượng";
            m_grv_san_pham.Columns[3].Width = 80;
            m_grv_san_pham.Columns[4].HeaderText = "Đơn vị tính";
            m_grv_san_pham.Columns[4].Width = 100;
            m_grv_san_pham.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        #endregion
       
        #region Events

        private void F501_Danh_muc_san_pham_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_them_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                if (check_data_validate_is_ok())
                {
                    DM_SAN_PHAM v_dm_san_pham = new DM_SAN_PHAM();
                    v_dm_san_pham.MA_SAN_PHAM = m_txt_ma_san_pham.Text.Trim();
                    v_dm_san_pham.TEN_SAN_PHAM = m_txt_ten_san_pham.Text.Trim();

                    //GD_DON_GIA v_gd_don_gia = new GD_DON_GIA();
                    //DM_NHA_CUNG_CAP v_dm_nha_cung_cap = new DM_NHA_CUNG_CAP();
                    //DM_LOAI_SAN_PHAM v_dm_loai_sp = new DM_LOAI_SAN_PHAM();
                    //v_dm_san_pham.MA_SAN_PHAM = m_txt_ma_san_pham.Text.Trim();
                    //v_dm_san_pham.TEN_SAN_PHAM = m_txt_ten_san_pham.Text.Trim();
                    //v_dm_san_pham.DON_VI_TINH = m_txt_don_vi_tinh.Text.Trim();
                    //string don_gia = v_gd_don_gia.DON_GIA_BAN.ToString();
                    //don_gia = m_txt_don_gia.Text.Trim();
                    //v_dm_nha_cung_cap.TEN_NCC = m_txt_ten_ncc.Text.Trim();
                    //v_dm_loai_sp.TEN_LOAI_SAN_PHAM = m_txt_ten_loai_san_pham.Text.Trim();
                    db.DM_SAN_PHAM.Add(v_dm_san_pham);
                    m_cmd_xoa_trang_Click(null, null);
                    db.SaveChanges();
                    MessageBox.Show("Bạn đã thêm sản phẩm thành công!", "Thông báo!");
                    load_data_to_grid();
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_cap_nhat_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_grv_san_pham.CurrentRow == null)
                    return;
                decimal id_san_pham = (decimal)m_grv_san_pham.CurrentRow.Cells[0].Value;
                var v_dm_san_pham = db.DM_SAN_PHAM.FirstOrDefault(x => x.ID == id_san_pham);
                if (v_dm_san_pham != null)
                {
                    if (check_data_validate_is_ok())
                    {
                        v_dm_san_pham.MA_SAN_PHAM = m_txt_ma_san_pham.Text.Trim();
                        v_dm_san_pham.TEN_SAN_PHAM = m_txt_ten_san_pham.Text.Trim();
                        v_dm_san_pham.DM_LOAI_SAN_PHAM.MA_LOAI_SAN_PHAM = m_txt_ten_loai_san_pham.Text.Trim();
                        v_dm_san_pham.DM_NHA_CUNG_CAP.TEN_NCC = m_txt_ten_ncc.Text.Trim();
                        v_dm_san_pham.DON_VI_TINH = m_txt_don_vi_tinh.Text.Trim();
                        db.SaveChanges();
                        m_cmd_xoa_trang_Click(null, null);
                        MessageBox.Show("Bạn đã cập nhật thành công sản phẩm!", "Thông báo!");
                        load_data_to_grid();
                    }
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_tim_kiem_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xoa_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_grv_san_pham.CurrentRow == null)
                {
                    MessageBox.Show("Bạn cần chọn sản phẩm để xóa!", "Thông báo!");
                    return;
                }
                decimal id_san_pham = (decimal)m_grv_san_pham.CurrentRow.Cells[0].Value;
                var v_dm_san_pham = db.DM_SAN_PHAM
                    .FirstOrDefault(x => x.ID == id_san_pham);
                if (v_dm_san_pham != null)
	            {
		            if (MessageBox.Show("Bạn có chắc chắn muốn XÓA SẢN PHẨM này và các DỮ LIỆU CÓ LIÊN QUAN không?", "Cảnh báo!", MessageBoxButtons.YesNo) == DialogResult.Yes)
	                {
		                //Xóa dữ liệu bảng GD_SO_DU
                        var v_lst_gd_so_du = db.GD_SO_DU
                            .Where(x => x.ID_SAN_PHAM == id_san_pham)
                            .ToList();
                        for (int i = 0; i < v_lst_gd_so_du.Count; i++)
			            {
			                db.GD_SO_DU.Remove(v_lst_gd_so_du[i]);
                            db.SaveChanges();
			            }
                         //Xóa dữ liệu bảng DM_SAN_PHAM
                        var v_lst_san_pham = db.DM_SAN_PHAM
                            .Where(x => x.ID == id_san_pham)
                            .ToList();
                        for (int i = 0; i < v_lst_san_pham.Count; i++)
                        {
                            db.DM_SAN_PHAM.Remove(v_lst_san_pham[i]);
                            db.SaveChanges();
                            MessageBox.Show("Bạn đã xóa dữ liệu thành công!", "Thông báo!");
                            load_data_to_grid();
                        }
	                }    
	            }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xoa_trang_Click(object sender, EventArgs e)
        {
            try
            {
                m_txt_ma_san_pham.Text = "";
                m_txt_ten_loai_san_pham.Text = "";
                m_txt_ten_san_pham.Text = "";
                m_txt_ten_ncc.Text = "";
                m_txt_don_vi_tinh.Text = "";
                m_txt_don_gia.Text = "";
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }
        
        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }
        #endregion      
    }
}